/**
 * File: EncomendaItensController.java
 * Data: 14.11.2010
 * Objective: Criar a classe controller da entidade Encomenda Itens.
 *
 * Copyright 2010 S2DZ CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controller;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.EncomendaItensModelo;

/**
 * Controller da entidade Encomenda Itens.
 * @author Paulo Ilenga
 */
public class EncomendaItensController extends ControllerGeneric
{
    private EncomendaItensModelo modelo;

    @Override
    public boolean save(Object object)
    {
        modelo = (EncomendaItensModelo) object;

        connect();

        sql = "INSERT INTO tbl_encomenda_itens(id_compra_header, id_produto, preco, imposto, quantidade) VALUES (?, ?, ?, ?, ?)";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setInt(1, modelo.getIdEncomendaHeader());
            ps.setInt(2, modelo.getIdProduto());
            ps.setFloat(3, modelo.getPreco());
            ps.setFloat(4, modelo.getImposto());
            ps.setInt(5, modelo.getQuatidade());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EncomendaItensController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

    @Override
    public boolean update(Object object)
    {
        modelo = (EncomendaItensModelo) object;

        connect();

        sql = "UPDATE tbl_encomenda_itens SET id_compra_header=?, id_produto=?, preco=?, imposto=?, quantidade=? WHERE id=?";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setInt(1, modelo.getIdEncomendaHeader());
            ps.setInt(2, modelo.getIdProduto());
            ps.setFloat(3, modelo.getPreco());
            ps.setFloat(4, modelo.getImposto());
            ps.setInt(5, modelo.getQuatidade());
            ps.setInt(6, modelo.getId());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EncomendaItensController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

}
